﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Marking.Dashboard.Infrastructures;
using Marking.Dashboard.Services;
using Marking.Dashboard.Views.Login;

namespace Marking.Dashboard.ViewModels.Sales
{
    public class SalesViewModel : ViewModelBase
    {
        private object _locker = new object();
        private ISalesService _saleService;

        public SalesViewModel(ISalesService salesService)
        {
            _saleService = salesService;

            this.CycleSearchViewModel = new CycleSearchViewModel();
            this.CycleSearchViewModel.SearchInfoChanged = (e) => { this.LoadSalesInfos(); };

            this.LoadSalesInfosCommand = new DelegateCommand<string>(this.LoadSalesInfos, this.CanLoadSalesInfos);

            this.LoadSalesInfos();
        }

        public CycleSearchViewModel CycleSearchViewModel { get; private set; }

        private SalesLineChartViewModel _salesLineChartViewModel;
        public SalesLineChartViewModel SalesLineChartViewModel
        {
            get { return _salesLineChartViewModel; }
            private set
            {
                _salesLineChartViewModel = value;

                this.NotifyPropertyChanged("SalesLineChartViewModel");
            }
        }

        private string _currentCommand = "布匹";
        public DelegateCommand<string> LoadSalesInfosCommand { get; set; }

        private void LoadSalesInfos(string command)
        {
            if (command == "布匹")
            {
                _currentCommand = command;
                this.LoadSalesInfosCommand.RaiseCanExecuteChanged();
                this.LoadSalesInfos();
            }
            else
            {
                var loginWindow = new LoginWindow(() =>
                {
                    _currentCommand = command;
                    this.LoadSalesInfosCommand.RaiseCanExecuteChanged();
                    this.LoadSalesInfos();
                });

                loginWindow.Show();
            }
        }

        private bool CanLoadSalesInfos(string command)
        {
            return command != _currentCommand;
        }

        private void LoadSalesInfos()
        {
            if (_currentCommand == "布匹")
            {
                this.LoadClothSalesInfos(this.CycleSearchViewModel);
            }
            else
            {
                this.LoadMoneySalesInfos(this.CycleSearchViewModel);
            }
        }

        private void LoadClothSalesInfos(CycleSearchViewModel cycleSearchViewModel)
        {
            _saleService.LoadClothSalesInfos(
                cycleSearchViewModel.CurrentCircleType,
                cycleSearchViewModel.StartDate,
                cycleSearchViewModel.EndDate,
                (salesInfos) =>
                {
                    lock (_locker)
                    {
                        this.SalesLineChartViewModel = new SalesLineChartViewModel("销售布匹走势", "匹")
                        {
                            ReportUrl = "/application/marketingsell/Pro_ReportSellForProductClass.htm"
                        };

                        foreach (var item in salesInfos)
                        {
                            this.SalesLineChartViewModel.Add(item);
                        }
                    }
                });
        }

        private void LoadMoneySalesInfos(CycleSearchViewModel cycleSearchViewModel)
        {
            _saleService.LoadMoneySalesInfos(
                cycleSearchViewModel.CurrentCircleType,
                cycleSearchViewModel.StartDate,
                cycleSearchViewModel.EndDate,
                (salesInfos) =>
                {
                    lock (_locker)
                    {
                        this.SalesLineChartViewModel = new SalesLineChartViewModel("销售收款走势", "元");

                        foreach (var item in salesInfos)
                        {
                            this.SalesLineChartViewModel.Add(item);
                        }
                    }
                });
        }
    }
}
